﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Copad.BusinessCard.Model;
using Copad.BusinessCard.BLL;

public partial class BackgrandManage_index : System.Web.UI.Page
{
    UserGroupBLL userGroupBLL = new UserGroupBLL();

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) { return; }
        var cookie = Request.Cookies["Username"];
        if (cookie != null)
        {
            this.UserName.Text = cookie.Value;
            this.Password.Focus();
        }
        else
        {
            this.UserName.Focus();
        }
    }

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //判断验证码
        if (Session["code"] != null)
        {
            string code = Session["code"].ToString().Trim();

            if (tbx_Code.Text.Trim().Equals(""))
            {
                ShowMessage("验证码不能为空!");
                return;
            }
            if (!code.Equals(tbx_Code.Text.Trim()))
            {
                ShowMessage("验证码错误!");
                return;
            }
        }

        //获得用登录信息
        String username = this.UserName.Text.Trim();
        String userPassword = this.Password.Text.Trim();
        UserInfo usersession = null;
        try
        {
            usersession = new UserBLL().ValidateUser(username, userPassword);
        }
        catch (Exception ex)
        {
            Manager.WriteLog(OperationEnum.Login, "登录验证时出现异常！<br/>" + ex.Message + "<br/>" + ex.StackTrace);
        }

        if (usersession != null)
        {
            WorkTimeManager workTime = Manager.TimeManager;
            int index = (int)DateTime.Now.DayOfWeek;
            if (index == 0) { index = 6; }
            else
            {
                index--;
            }
            TimeSpan begin, end;
            if (!TimeSpan.TryParse(workTime.BeginTime[index], out begin))
            {
                begin = new TimeSpan(0, 0, 0);
            }
            if (!TimeSpan.TryParse(workTime.EndTime[index], out end))
            {
                end = new TimeSpan(23, 59, 59);
            }

            double current = DateTime.Now.TimeOfDay.TotalMilliseconds;
            bool searched = false;

            if (current > end.TotalMilliseconds || current < begin.TotalMilliseconds)
            {
                foreach (var item in workTime.DeniedGroupID)
                {
                    foreach (var g in usersession.Groups)
                    {
                        if (g.ID == item)
                        {
                            searched = true; break;
                        }
                    }
                }
                searched = searched | workTime.DeniedUserID.Contains(usersession.User.ID);
            }

            if (searched)
            {
                Manager.WriteLog(OperationEnum.Login, string.Format("用户：{0} 尝试在禁止的时间段内登录，以拒绝!", username));
                ShowMessage("您的用户在此时间段内已被禁止登录！");
            }
            else
            {
                //登录用户信息
                var cookie = Request.Cookies["Username"];
                if (cookie == null)
                {
                    cookie = new HttpCookie("Username", usersession.User.Username);
                }
                else
                {
                    cookie.Value = usersession.User.Username;
                }
                Response.SetCookie(cookie);
                Manager.SetLogined(usersession, Session);
                Response.Write("<script>if(window.parent){window.parent.location='index.aspx';}else{window.location='index.aspx';}</script>");
                Manager.WriteLog(OperationEnum.Login, string.Format("用户：{0} 登录成功!", username));
                //Tool.InsertLog(Request.UserHostAddress, usersession.LoginName, "登录用户名为" + usersession.LoginName, "0", usersession.GongshangsIDS, usersession.GongshangsName, usersession.FenJuName, usersession.ShiJuName);
            }
        }
        else
        {
            ShowMessage("登录失败!请检查用户名密码是否正确!");
            Manager.WriteLog(OperationEnum.Login, string.Format("用户：{0} 尝试登录失败!", username));
            tbx_Code.Text = "";
        }
    }
    /// <summary>
    /// 向浏览器输出对话框
    /// </summary>
    /// <param name="Text"></param>
    public void Alert(object Text)
    {
        string str = "<Script language='JavaScript'>window.alert('" + Text.ToString() + "')</Script>";
        Response.Write(str);
    }

    //public void OpenNewUrl(string Url)
    //{
    //    string str = "<Script language='JavaScript'>window.open('" + Url + "','_self')</Script>";
    //    Response.Write(str);
    //}

    private void ShowMessage(string message)
    {
        this.Label1.Text = message;
        this.Label1.Visible = true;
        this.tbx_Code.Text = "";
    }

}
